using UnityEngine;

[ExecuteInEditMode]
public class ControllerDecoration : ControllerGeneric
{
	public float length = 1f;

	public float width = 1f;

	public float height = 1f;

	public GameObject cub;

	private bool staticState;

	public override void OnLevelInPlace(Transform levelTransform)
	{
		base.OnLevelInPlace(levelTransform);
		if (levelTransform == base.transform.root)
		{
			PrepareForGame();
		}
	}

	public void PrepareForGame()
	{
		SetNames();
		SetParenting();
		base.name = "will_destroy_" + base.name;
		if (Application.isPlaying)
		{
			Object.Destroy(base.gameObject);
		}
		else
		{
			Object.DestroyImmediate(base.gameObject);
		}
	}

	private void Update()
	{
		if (!Application.isEditor || Application.isPlaying)
		{
			return;
		}
		SetupRemoteControl();
		if (IsRemoteControlActive())
		{
			base.transform.position = remoteControl.transform.position;
			base.transform.rotation = remoteControl.transform.rotation;
			length = remoteControl.transform.localScale.z;
			width = remoteControl.transform.localScale.x;
			height = remoteControl.transform.localScale.y;
			base.transform.localScale = Vector3.one;
		}
		if (height < 0.0001f)
		{
			height = 0.0001f;
		}
		if (length < 0.0001f)
		{
			length = 0.0001f;
		}
		if (width < 0.0001f)
		{
			width = 0.0001f;
		}
		if (remoteControl != null)
		{
			if (remoteControl.manualScale)
			{
				remoteControl.scale = new Vector3(remoteControl.scale.x, remoteControl.scale.y, remoteControl.scale.z);
			}
			else
			{
				remoteControl.transform.localScale = new Vector3(width, height, length);
			}
		}
		Setup();
		SetupUVs();
	}

	private void Setup()
	{
		AssureParenting();
		SetNames();
		cub.transform.localPosition = new Vector3(0f, (0f - height) * 0.5f, 0f);
		cub.transform.localScale = new Vector3(width, height * 4f, length);
	}

	private void SetStatic(bool newStaticState = true)
	{
		if (newStaticState != staticState)
		{
			staticState = newStaticState;
			cub.isStatic = staticState;
		}
	}

	private void SetParenting()
	{
		for (int childCount = base.transform.childCount; childCount > 0; childCount = base.transform.childCount)
		{
			base.transform.GetChild(0).parent = base.transform.parent;
		}
	}

	private void SetNames()
	{
		cub.name = "Decoration";
	}

	public void SetupRemoteControl()
	{
		if (remoteControl == null)
		{
			InitRemoteControl();
			remoteControl.gameObject.name = remoteControl.gameObject.name + base.name;
			remoteControl.gameObject.transform.position = base.transform.position;
			remoteControl.gameObject.transform.rotation = base.transform.rotation;
			remoteControl.gameObject.transform.localScale = new Vector3(width, height, length);
		}
	}

	public override void CopyFromRemoteControl(RemoteControl rc)
	{
		base.CopyFromRemoteControl(rc);
		CopyParentingFromRemoteControl(rc);
	}

	public void SetupUVs()
	{
		Mesh mesh = cub.GetComponent<MeshFilter>().mesh;
		Vector2[] uv = mesh.uv;
		float num = 0.125f;
		float num2 = 0.125f;
		uv[4].x = 0.5f + width * num;
		uv[6].x = 0.5f - width * num;
		uv[5].x = 0.5f - width * num;
		uv[7].x = 0.5f + width * num;
		uv[4].y = 0.5f + length * num2;
		uv[6].y = 0.5f + length * num2;
		uv[5].y = 0.5f - length * num2;
		uv[7].y = 0.5f - length * num2;
		uv[12].x = 0.5f - width * num;
		uv[14].x = 0.5f + width * num;
		uv[13].x = 0.5f + width * num;
		uv[15].x = 0.5f - width * num;
		uv[12].y = 0.5f - height * num2;
		uv[14].y = 0.5f - height * num2;
		uv[13].y = 0.5f + height * num2;
		uv[15].y = 0.5f + height * num2;
		uv[8].x = 0.5f + width * num;
		uv[10].x = 0.5f - width * num;
		uv[9].x = 0.5f - width * num;
		uv[11].x = 0.5f + width * num;
		uv[8].y = 0.5f - height * num2;
		uv[10].y = 0.5f - height * num2;
		uv[9].y = 0.5f + height * num2;
		uv[11].y = 0.5f + height * num2;
		uv[16].x = 0.5f + length * num;
		uv[18].x = 0.5f - length * num;
		uv[17].x = 0.5f - length * num;
		uv[19].x = 0.5f + length * num;
		uv[16].y = 0.5f - height * num2;
		uv[18].y = 0.5f - height * num2;
		uv[17].y = 0.5f + height * num2;
		uv[19].y = 0.5f + height * num2;
		uv[20].x = 0.5f - length * num;
		uv[22].x = 0.5f + length * num;
		uv[21].x = 0.5f + length * num;
		uv[23].x = 0.5f - length * num;
		uv[20].y = 0.5f - height * num2;
		uv[22].y = 0.5f - height * num2;
		uv[21].y = 0.5f + height * num2;
		uv[23].y = 0.5f + height * num2;
		mesh.uv = uv;
	}
}
